/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Cover;
import java.io.ByteArrayInputStream;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Session;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Fess
 */
public class CoverIMPL implements CoverDAO{
    
    @Override
    public void addCover(Cover cover) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(cover);
            session.getTransaction().commit();
            System.out.println("HiB: Cover saved");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover save error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public Cover getCoverByID(Long id) throws SQLException {
        Session session=null;
        Cover cover=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            cover=(Cover)session.load(Cover.class, id);
            System.out.println("HiB: Cover taken");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover take error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return cover;
    }

    @Override
    public void updateCover(Long cover_id, Cover cover) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(cover);
            session.getTransaction().commit();
            System.out.println("HiB: Cover updated");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover updating failed");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Cover> getAllCover() throws SQLException {
        Session session=null;
        List<Cover> list=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            list=(List<Cover>)session.createCriteria(Cover.class).list();
            System.out.println("HiB: Cover List taken");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover take error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return list;
    }

    @Override
    public void deleteCover(Cover cover) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(cover);
            session.getTransaction().commit();
            System.out.println("HiB: Cover deleted");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover deleting error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public StreamedContent getCoverImage(Long id,int imageType) throws SQLException {
        Session session=null;
        Cover cover=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            cover=(Cover)session.load(Cover.class, id);
            System.out.println("HiB: Cover taken");
        }
        catch(Exception ex){
            System.err.println("HiB: Cover take error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        byte[] b=null;
        if(cover==null)return null;
        switch(imageType){
            case 1: b=cover.getImage(); break;
            case 2: b=cover.getSmallImage(); break;
        }
        StreamedContent tempStream=new DefaultStreamedContent(new ByteArrayInputStream(b), "image/jpg");
        return tempStream;
    }
    
}
